package com.finger.dao;

import com.finger.pojo.*;
import com.finger.pojo.dto.LearnedWordDto;
import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Mapper
public interface WordDao {
    //一次性获取所有单词数据，1.文本数据很小，2.一次拿过来完，减轻数据库连接压力
    @Select("select * from words where source = #{source}")
    public List<Word> getAllWords(User user) throws NullPointerException;

    //根据用户id查询该用户所有学习过的单词
    @Select("select * from user_learned_words where uid = #{id} and source = #{source}")
    public List<UserLearnedWord> getAllLearnedWords(User user) throws NullPointerException;

    //根据用户id和单词id提交一条学习记录
    @Insert("insert into user_learned_words (uid,wid,count,lastSubmissionTime,lastSubmissionInterval,source) values (#{uid},#{wid},#{count},#{lastSubmissionTime},#{lastSubmissionInterval},#{source})")
    Boolean submitWordByUidAndWid(UserLearnedWord userLearnedWord);

    //根据用户id和单词id查询当前单词是否学习过
    @Select("select count from user_learned_words where uid = #{uid} and wid = #{wid}")
    Integer getByUidAndWid(@Param("uid") Integer uid,@Param("wid") Integer wid) throws NullPointerException;

    //更新user_learned_words表中已经学过的单词
    @Update("update user_learned_words set count=#{count},lastSubmissionTime=#{lastSubmissionTime},lastSubmissionInterval=#{lastSubmissionInterval} ,source = #{source} where uid = #{uid} and wid = #{wid}")
    Boolean updateSubmitWord(UserLearnedWord userLearnedWord);

    //获取所有提示语信息
    @Select("select * from prompt")
    public List<Prompt> getAllPrompts();

    //根据用户id和单词id获取当前用户当前已经学习单词信息
    @Select("select * from user_learned_words where uid = #{uid} and wid = #{wid}")
    public UserLearnedWord getUserLearnedWordByUidAndWid(@Param("uid") Integer uid,@Param("wid") Integer wid);

    //根据单词id获取当前单词信息
    @Select("select * from words where id = #{wid};")
    public Word getWordByWid(@Param("wid") Integer wid);

    //根据用户id查询当前用户已经刷了多少遍所有单词
    @Select("select Min(count) from user_learned_words where uid = #{id}")
    public int getMinWordsLearnCount(@Param("id") Integer id);

    //根据用户id查询用户学习单词次数
    @Select("select SUM(count) from user_learned_words where uid = #{id} and source = #{source}")
    public int getLearnCount(User user);

    //根据用户id获取最近学习过的十个单词
    @Select("select distinct w.id as id,w.value as wordValue,w.explain as wordExplain, l.count as learnedCount,l.lastSubmissionTime as lastSubmissionTime from user_learned_words l left join words w on l.wid = w.id where uid = #{uid} ORDER BY lastSubmissionTime desc limit 10;")
    public List<LearnedWordDto> getUserLearnedWords10ByUid(Integer uid);

    // 通过用户id和单词id，查询当前单词是否处于收藏状态
    @Select("select id from collection where uid = #{uid} and wid = #{wid};")
    public Integer getWordCollectionStatus(@Param("uid") Integer uid,@Param("wid") Integer wid);

    // 收藏单词
    @Insert("insert into collection (id,uid,wid) values (null,#{uid},#{wid});")
    public Boolean createCollectionByUidAndWid(@Param("uid") Integer uid,@Param("wid") Integer wid);

    // 取消收藏
    @Delete("delete from collection where id = #{id};")
    public Boolean deleteCollectionById(Integer id);

    // 获取当前用户收藏的所有单词
    @Select("select w.* from words w,collection c where c.uid = #{id} and c.wid = w.id;")
    public List<Word> getCollectionWords(Integer id);
    // 获取用户当前的词源信息
    @Select("select * from source where source_id = #{id};")
    public Source getSourceById(Integer id);

    // 获取当前用户所有词源信息
    @Select("SELECT DISTINCT \n" +
            "s.source_id as source_id,\n" +
            "s.name as name,\n" +
            "s.scope as scope\n" +
            "from source s\n" +
            "LEFT JOIN user u on s.scope = u.id \n" +
            "where s.scope = u.id \n" +
            "and u.id = #{uid}\n" +
            "UNION\n" +
            "SELECT \n" +
            "source_id as source_id,\n" +
            "name as name,\n" +
            "scope as scope\n" +
            "from source s\n" +
            "WHERE s.scope = 0")
    public List<Source> getUserSourceInfo(Integer uid);

    @Select("SELECT DISTINCT s.source_id," +
            "s.name,s.scope," +
            "u.username as create_user_name " +
            "from source s\n" +
            "LEFT JOIN user u on s.scope = u.id;")
    public List<Source> adminGetUserSourceInfo();

    // 获取当前词源下的所有单词信息
    @Select("SELECT * FROM words w WHERE w.source = #{source};")
    public List<Word> getWordsBySource(Integer source);

    // 添加一条单词信息
    @Select("insert into words VALUES (null,#{value},#{explain},null,null,null,null,null,#{source});")
    public Boolean addWord(Word word);
    // 删除一条单词信息
    @Delete("DELETE from words WHERE id = #{id};")
    public Boolean deleteWord(Integer id);

    // 修改一条单词信息
    @Update("update words set `explain` =#{explain} where id =#{id};")
    public Boolean updateWordExplain(Word word);

    @Update("update words set value =#{value} where id =#{id};")
    public Boolean updateWordValue(Word word);

    @Update("update words set source =#{source} where id =#{id};")
    public Boolean updateWordSource(Word word);

    // 模糊查询单词信息
    @Select("SELECT * from words where value like #{searchKey};")
    public Boolean selectWord(String searchKey);



}
